package com.wzw.springboot.principle.use.security.infrastructure.handle;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author Wangzhiwen
 */
@Component
public class PasswordEncoderHandler implements ApplicationContextAware {

    private static PasswordEncoder PASSWORD_ENCODER;

    private PasswordEncoderHandler() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        PASSWORD_ENCODER = applicationContext.getBean(PasswordEncoder.class);
    }

    public static String encode(CharSequence rawPassword) {
        return PASSWORD_ENCODER.encode(rawPassword);
    }

    public static boolean matches(CharSequence rawPassword, String encodedPassword) {
        return PASSWORD_ENCODER.matches(rawPassword, encodedPassword);
    }

    public static boolean upgradeEncoding(String encodedPassword) {
        return PASSWORD_ENCODER.upgradeEncoding(encodedPassword);
    }
}
